

//
//  LJHListDetailViewController.m
//  HEARME
//
//  Created by liujinhui on 16/1/28.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LJHListDetailViewController.h"
#import "LJHListTrackTableViewCell.h"
#import "LJHListAthourTableViewCell.h"
#import "LJHListAlbumTableViewCell.h"
#import "LJHAuthorDetailViewController.h"
#import "LJHPlayerViewController.h"
#import "LJHAnchorThreeModel.h"
#import "LJHAuthorDetailViewController.h"
#import "LJHAlbumsDetailViewController.h"
@interface LJHListDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)LJHBaseTableView *myTableView;
@property(nonatomic,retain)NSMutableArray *myArray;

@end

@implementation LJHListDetailViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setTranslucent:NO];
    [[LJHPlayBtn shareButton].musicBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [LJHPlayBtn shareButton].frame =CGRectMake(WIDTH*0.4, HEIGHT-WIDTH*0.2-64, WIDTH*0.2, HEIGHT*0.2);
    [self.view addSubview :[LJHPlayBtn shareButton]];
    if ([LJHPlayerViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePlaying) {
        
        
        [[LJHPlayBtn shareButton] startCircle];
    }
    if ([LJHPlayerViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePaused) {
        
        [[LJHPlayBtn shareButton] stopCircle];
        
    }
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.myArray = [NSMutableArray array];
    
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0 , 20, 30);
    backButton.backgroundColor = [UIColor clearColor];
    [backButton setImage:[UIImage imageNamed:@"btn_back_n@2x.png"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"btn_back_h@2x.png"] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    
    [self getData];
    [self createTableView];

}


#pragma mark - 返回按钮方法
-(void)backButtonAction:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)buttonAction:(UIButton *)btn
{
    
    LJHPlayerViewController *player = [LJHPlayerViewController sharaMusicPlayer];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:player];
    [self.view.window.rootViewController presentViewController:naVC animated:YES completion:^{
        
    }];
    
}

#pragma mark - 请求数据

-(void)getData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/%@?device=iphone&key=%@&pageId=1&pageSize=40", self.contentType, self.key];
//    http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/track?device=android&key=ranking%3Atrack%3Aplayed%3A1%3A0&pageId=2&pageSize=40
    
    [LJHAppTools afnGetRequest:urlStr block:^(id result) {
        
        NSMutableArray *array = [result objectForKey:@"list"];
        
        for (NSMutableDictionary *dic in array) {
            if ([_contentType isEqualToString:@"track"]) {
                LJHTrackModel *model = [LJHTrackModel modelWithDictionary:dic];
                [self.myArray addObject:model];
                
            }
            else if ([_contentType isEqualToString:@"album"])
            {
                LJHAlbumModel *model = [LJHAlbumModel modelWithDictionary:dic];
                [self.myArray addObject:model];
            }
            else{
                
                LJHAnchorThreeModel *model = [LJHAnchorThreeModel modelWithDictionary:dic];
                [self.myArray addObject:model];
            }
        }

        [self.myTableView reloadData];
    } error:^(NSError *error) {
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"您的设备无网络访问" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        
        UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertC addAction:cancelButton];
        
    }];
}

#pragma mark - 创建tableView

-(void)createTableView
{
    self.myTableView = [[LJHBaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    [self.myTableView registerClass:[LJHListAlbumTableViewCell class] forCellReuseIdentifier:@"listAlbumTableCell"];
    [self.myTableView registerClass:[LJHListAthourTableViewCell class] forCellReuseIdentifier:@"listAthourTableCell"];
    [self.myTableView registerClass:[LJHListTrackTableViewCell class] forCellReuseIdentifier:@"listTrackTableCell"];
    self.myTableView.bounces = NO;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

#pragma mark - tableView协议方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.myArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJHListTrackTableViewCell *trackCell = [tableView dequeueReusableCellWithIdentifier:@"listTrackTableCell"];
    LJHListAlbumTableViewCell *albumCell = [tableView dequeueReusableCellWithIdentifier:@"listAlbumTableCell"];
    LJHListAthourTableViewCell *athour = [tableView dequeueReusableCellWithIdentifier:@"listAthourTableCell"];
    
    if ([_contentType isEqualToString:@"track"])
    {
        LJHTrackModel *model = [self.myArray objectAtIndex:indexPath.row];
        trackCell.textModel = model;
        trackCell.numLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
        return trackCell;
    }
    else if ([_contentType isEqualToString:@"album"])
    {
        LJHAlbumModel *model = [self.myArray objectAtIndex:indexPath.row];
        albumCell.albumModel = model;
        albumCell.numLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
        return albumCell;
    }
    else{
        
        LJHAnchorThreeModel *model = [self.myArray objectAtIndex:indexPath.row];
        athour.textModel = model;
        athour.numLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
        
        return athour;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT*0.16;
}

#pragma mark - 点击跳转方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([_contentType isEqualToString:@"track"])
    {
        LJHTrackModel *model = [self.myArray objectAtIndex:indexPath.row];
        LJHPlayerViewController *player = [LJHPlayerViewController sharaMusicPlayer];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:player];
        player.song_id = model.trackId;
        player.allMusicArray = self.myArray;
        player.indexRow = indexPath.row;
        [self presentViewController:naVC animated:YES completion:^{
            
        }];
    }
    else if ([_contentType isEqualToString:@"album"])
    {
        LJHAlbumModel *model = [self.myArray objectAtIndex:indexPath.row];
        
        LJHAlbumsDetailViewController *albumsDetailVC = [[LJHAlbumsDetailViewController alloc] init];
        
        albumsDetailVC.navigationController.navigationBar.translucent = YES;
        
        albumsDetailVC.albumId = model.myId;
        //        albumsDetailVC.myTitle = myTitle;
        albumsDetailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:albumsDetailVC animated:YES];
        
    }
    else
    {
        
        LJHAlbumModel *model = [self.myArray objectAtIndex:indexPath.row];
        LJHAuthorDetailViewController *authorDetailVC = [[LJHAuthorDetailViewController alloc] init];
        
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:authorDetailVC];
        
        authorDetailVC.uid = model.uid;

        [self.view.window.rootViewController presentViewController:naVC animated:YES completion:^{
            
            
        }];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
